package com.yw.master.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * (拦截器)模块 TODO(访问拦截规则)
 * 
 * @author devintam
 * @date 2015年7月13日 下午12:49:24
 *
 */
public class UrlCheckInterceptor implements HandlerInterceptor {

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception ex) throws Exception {

		String requestRri = request.getRequestURI();
		String uriPrefix = request.getContextPath();
		if ((StringUtils.startsWith(requestRri, uriPrefix) && (StringUtils.endsWith(requestRri, "/save") || StringUtils.endsWith(requestRri, "/update")
				|| StringUtils.endsWith(requestRri, "/list") || StringUtils.endsWith(requestRri, "/view") || StringUtils.endsWith(requestRri, "/form")
				|| StringUtils.endsWith(requestRri, "/delete") || StringUtils.endsWith(requestRri, "/import")
				|| StringUtils.endsWith(requestRri, "/sqsearch") || StringUtils.endsWith(requestRri, "/updateSort")))
				|| ex != null) {
//			SysLogUtil.saveLog(request, ex == null ? Constants.TYPE_ACCESS : Constants.TYPE_EXCEPTION, ex == null ? "" : ex.getMessage());
		}
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav) throws Exception {

	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		String requestRri = request.getRequestURI();
		String uriPrefix = request.getContextPath();
		String otherRri = requestRri.substring(uriPrefix.length());
		if (StringUtils.startsWith(requestRri, uriPrefix)
				&& (StringUtils.endsWith(requestRri, "/") || StringUtils.endsWith(requestRri, "/login") || StringUtils.startsWith(otherRri, "/api/v1")
						|| StringUtils.startsWith(otherRri, "/images") || StringUtils.startsWith(otherRri, "/jquery")
						|| StringUtils.startsWith(otherRri, "/css") || StringUtils.endsWith(otherRri, "/validateCodeServlet")
						|| StringUtils.endsWith(requestRri, "/index") || StringUtils.endsWith(requestRri, "/sysLogin"))) {
			return true;
		}
//		return UserUtil.getUser() == null ? false : true;
		return true;
	}
}
